-- shop
-- created by panyl
-- 地牢格子：商店类

return {
    restoreGrid = function(grid, info)
        local goods = info.dungeon_goods;
        if not goods or #goods <= 0 then
            return {};
        end

        local ret = {};
        local shopType = grid.shopType .. grid:getPos();

        for index, data in pairs(goods) do
            local t = {};

            if data.goodsId then
                t.goodsId = data.goodsId;
            end
            if data.goods_id then
                t.goodsId = data.goods_id;
            end

            if (data.soldOut) then
                t.soldOut = data.soldOut;
            end
            if (data.sold_out) then
                t.soldOut = data.sold_out;
            end

            ret[index] = t;
        end

        DungeonShopM.updateCurrentSellGoods(shopType, ret);
    end,

    -- 收集上下文
    collectContext = function(grid)
        local ret = {};

        local path = grid.shopType .. grid:getPos();

        local goods = table.deepcopy(DungeonShopM.getSellGoodsByType(path));
        local temp = {};

        for index, data in pairs(goods) do
            local t = {};

            if data.goodsId then
                t.goods_id = data.goodsId;
            end

            if (data.soldOut) then
                t.sold_out = data.soldOut;
            end

            temp[index] = t;
        end

        ret.dungeon_goods = temp;

        return ret;
    end,

    -- 自动战斗
    autoFight = function(grid)
        local path = grid.shopType .. grid:getPos();
        local goods = DungeonShopM.getSellGoodsByType(path);

        if grid.classId == 221 then
            -- 德古拉展柜
            if ItemM.getAmount(ME.user, 7240) <= 0 then
                return;
            end

            -- 随机找到一个可以兑换的
            while true do
                -- 九件圣器
                local index = math.random(#table.keys(goods));
                if DungeonActionM.go("buy_goods", grid:getPos(), index) then
                    return true;
                end
            end
        end

        for index, data in pairs(goods) do
            if DungeonShopM.canBuyGoods(path, index) == true then
                -- 买东西
                DungeonActionM.go("buy_goods", grid:getPos(), index)
            end
        end
    end,
};
